package com.y.fund.aacore.data;

import java.io.Serializable;

import com.y.fund.utils.DateTimeUtil;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@Schema(title = "API数据包装类")
public class DataVo<T> implements Serializable {

	private static final long serialVersionUID = -141344508202477180L;

	/** 操作成功时返回的代码值 */
    public static final int SUCCESSED = 10000;

    /** General error identification */
    public static final int FAILED = 40000;
    
    /** Not logged in or expired; 41xxx permission error */
    public static final int NOT_LOGIN = 41100;
    
    /** The operation is rejected; 41xxx permission error */
    public static final int OPT_REFUSE = 41103;
    
    /** No authority; 412xx operation permission error  */
    public static final int NOT_PERM = 41200;
    
    /** There are mandatory parameters that are not filled in; 42xxx data error  */
    public static final int MUST_PARAM = 42101;

    /** The parameter to be requested does not exist; 421xx requestor data error  */
    public static final int NOT_API = 42102;
    
    /** The key attribute values of the data to be saved cannot be duplicated [database already exists]; 421xx requestor data error */
    public static final int EXIST_ATTR_VAL = 42104;
    
    /** The data to be operated has expired or does not exist; 422xx response end data error  */
    public static final int NOT_DATA = 42200;

    /** The data to be operated has been referenced; 422xx response end data error */
    public static final int EXIST_QUOTE = 42202;

	public static final int DEVICE_DATA_PARSE_ERROR = 43000;

	public static final int DEVICE_DATA_VALIDATE_ERROR = 43001;

	@Schema(title = "返回结果代码；10000表示正常，其他值表示错误状态。41100：未登录或无效；42101:未填写必填参数；42104:不允许重复的键属性值；42200:数据已过期或不存在；42202:引用的数据")
    protected int code=0;
    
	@Schema(title = "错误描述；返回异常时的错误描述")
    protected String errMsg;

	@Schema(title = "响应时间戳；单位：毫秒")
    protected String timestamp = DateTimeUtil.NowTime();
    
	@Schema(title = "返回的业务数据，在添加时返回新数据的ID字符串；查询详细信息时返回对象的JSON字符串；列表查询返回数据数组")
	protected T data;

	public DataVo() {
		super();
        this.code = SUCCESSED;
    }
	
	public DataVo(int code, String errMsg) {
		super();
        this.code = code;
		this.errMsg = errMsg;
	}

	public static DataVo Success(){
		return Success(null);
	}
	
	public static<T> DataVo<T> Success(T data){
		if(null!=data && (data instanceof PaginationList)) {
			return new DataVo4Paging((PaginationList)data);
		}else {
			DataVo<T> dv = new DataVo<T>();
			dv.setCode(SUCCESSED);
			dv.setData(data);
			return dv;
		}
	}

	/**  分页数据传入调用 */
	public static DataVo Pagination(PaginationList data){
		return new DataVo4Paging(data);
	}
	
	public static DataVo Fail(String errMsg){
		DataVo dv = new DataVo();
		dv.setCode(FAILED);
		dv.setErrMsg(errMsg);
		
		return dv;
	}
	
	public static DataVo Fail(int errCode, String errMsg){
		DataVo dv = new DataVo();
		dv.setCode(errCode);
		dv.setErrMsg(errMsg);

		return dv;
	}

	public static DataVo Fail(int errCode, String errMsg, Object data){
		DataVo dv = new DataVo();
		dv.setErrMsg(errMsg);
		if(null!=data && (data instanceof PaginationList)) {
			return new DataVo4Paging(errCode, (PaginationList)data);
		}else {
			dv.setCode(errCode);
			dv.setData(data);
			return dv;
		}
	}
	
	public static DataVo FailNotLgn(){
		DataVo dv = new DataVo();
		dv.setCode(NOT_LOGIN);
		dv.setErrMsg("未登录或登录信息已失效");
		return dv;
	}
	public static DataVo FailNotData(){
		DataVo dv = new DataVo();
		dv.setCode(NOT_DATA);
		dv.setErrMsg("数据不存在");
		return dv;
	}
	public static DataVo FailNotData(String errMsg){
		DataVo dv = new DataVo();
		dv.setCode(NOT_DATA);
		dv.setErrMsg(errMsg);
		return dv;
	}

	public static DataVo FailMustParam(String attrNames){
		DataVo dv = new DataVo();
		dv.setCode(MUST_PARAM);
		dv.setErrMsg((null!=attrNames ? attrNames +" 不能为空" : "有必填参数为空！"));
		return dv;
	}
	
	public static DataVo FailExistAttrVal(String attrNames){
		DataVo dv = new DataVo();
		dv.setCode(EXIST_ATTR_VAL);
		dv.setErrMsg("字段 " + (null!=attrNames ? attrNames : "") + " 的值已经存在");
		
		return dv;
	}
	public static DataVo FailExistQuote(){
		return FailExistQuote(null);
	}
	
	public static DataVo FailExistQuote(String msg){
		DataVo dv = new DataVo();
		dv.setCode(EXIST_QUOTE);
		dv.setErrMsg(null==msg?"操作的数据存在引用，请先处理数据关联关系后再进行操作！":msg);
		
		return dv;
	}
	public static DataVo FailOperErr(){
		return FailOperErr(null);
	}
	public static DataVo FailOperErr(String errMsg){
		DataVo dv = new DataVo();
		dv.setCode(FAILED);
		dv.setErrMsg(null==errMsg ? "错误信息:" : errMsg);
		
		return dv;
	}

	public static DataVo FailDB(){
		DataVo dv = new DataVo();
		dv.setCode(FAILED);
		dv.setErrMsg("数据库操作失败，请稍后重试！");
		return dv;
	}

	/** 判断返回的DataVo对象是否为成功状态  */
	public static boolean IsSuc(DataVo dvo) {
		return null != dvo && DataVo.SUCCESSED == dvo.getCode();
	}
}
